// pages/scan/tmp/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: '', show_code: '-', uid: 0,
    id: 0, title: '-', detail: '-',
    cate_id: 0, cate_name: '-',
    thumb: '', visit_total: 0, lasttime: '-',
    pop_tmp: 0, pop_cate: 0, pop_attr: 0, //弹窗是否显示
    cateList: [], attrList: [],
    attrId: 0, attrKey: '', attrVal: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options);
    this.getOne(options);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 详情
  getOne: function (e) {
    var id = e.id ? e.id : 0;
    var code = e.code ? e.code : '';
    // console.log(id, code); return
    var uri = code=='' ? 'onebyid' : 'one';
    var _this = this;
    wx.request({
      method: 'POST',
      url: app.globalData.xcxApi + 'yw-user/tmp/' + uri,
      data: { code: code, id: id, },
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res); //return
        if (res.code < 0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        _this.setData({
          id: res.data.id,
          code: e,
          show_code: res.data.show_code,
          title: res.data.title, 
          detail: res.data.detail,
          cate_pid: res.data.cate_pid, 
          cate_pname: res.data.cate_pname,
          cate_id: res.data.cate_id, 
          cate_name: res.data.cate_name,
          visit_total: res.data.visit_total, 
          lasttime: res.data.lasttime,
          thumb: res.data.thumb, 
          attrs: res.data.attrs,
          pop_cate: 0, pop_attr: 0,
          pop_tmp: 0,
        });
      },
      fail: function () {
        console.log('error');
      }
    });
  },

  // 弹窗修改
  popEdit: function (e) {
    this.setData({
      pop_tmp: 1,
    });
  },

  // 修改标题
  toTitle: function (e) {
    console.log(e);
    this.setData({
      title: e.detail.value,
    });
  },

  // 修改描述
  toDetail: function (e) {
    console.log(e);
    this.setData({
      detail: e.detail.value,
    });
  },

  // 弹窗分类
  toSave: function (e) {
    var _this = this;
    var formData = {
      tmpid: this.data.id,
      title: this.data.title,
      detail: this.data.detail,
    };
    wx.request({
      method: 'POST',
      url: app.globalData.xcxApi + 'yw-user/tmp/modify',
      data: formData,
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res); //return
        if (res.code < 0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        _this.getOne(_this.data.code);
      },
      fail: function () {
        console.log('error');
      }
    });
  },

  // 选择图片
  chooseImage: function () {
    var _this = this;
    wx.chooseMedia({
      count: 1, // 最多选择1张
      mediaType: ['image'], // 只允许选择图片
      sourceType: ['album', 'camera'], // 相册和相机
      success: (res) => {
        const tempFilePath = res.tempFiles[0].tempFilePath;
        console.log(tempFilePath);
        // this.setData({
        //   tempFilePath: tempFilePath // 临时文件路径，用于预览
        // });
        wx.showLoading({
          title: '上传中...',
        });
        wx.uploadFile({
          url: app.globalData.xcxApi + 'yw-user/tmp/img/upload', 
          filePath: tempFilePath,
          name: 'img', 
          formData: {
            tmpid: this.data.id,
          },
          success(res) {
            console.log(res);
            wx.hideLoading();
            res = JSON.parse(res.data); // 后端返回的数据
            if (res.code === 0) {
              wx.showToast({
                title: '上传成功',
                icon: 'success'
              });
              _this.getOne(_this.data.code);
            } else {
              wx.showToast({
                title: '上传失败',
                icon: 'none'
              });
            }
          },
          fail(err) {
            wx.hideLoading();
            wx.showToast({
              title: '上传失败err', icon: 'none'
            });
          }
        });
      }
    });
  },

  // 弹窗分类
  toCate: function (e) {
    var _this = this;
    wx.request({
      method: 'POST',
      url: app.globalData.xcxApi + 'yw-user/tmp/cate/list',
      data: {  },
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res); //return
        if (res.code < 0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        _this.setData({
          cateList: res.data,
          pop_cate: 1,
        });
      },
      fail: function () {
        console.log('error');
      }
    });
  },

  // 选择分类
  toCateid: function (e) {
    console.log(e);
    var id = e.currentTarget.dataset.id;
    var _this = this;
    wx.request({
      method: 'POST',
      url: app.globalData.xcxApi + 'yw-user/tmp/cate/edit',
      data: { id: this.data.id, cate_id: id, },
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res); //return
        if (res.code < 0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        // _this.setData({
        //   pop_cate: 0,
        //   cate_id: id,
        //   cate_name: res.data.title,
        // });
        _this.getOne(this.data.code);
      },
      fail: function () {
        console.log('error');
      }
    });
  },

  // 属性添加弹窗
  popAttrAdd: function (e) {
    this.setData({
      pop_attr: 1,
      attrId: 0, attrKey: '', attrVal: '',
    });
  },

  // 属性修改弹窗
  popAttrEdit: function (e) {
    console.log(e);
    var id = e.currentTarget.dataset.id;
    var key = e.currentTarget.dataset.key;
    var val = e.currentTarget.dataset.val;
    this.setData({
      attrId: id,
      attrKey: key,
      attrVal: val,
      pop_attr: 1,
    });
  },

  // 属性名
  toAttrKey: function (e) {
    console.log(e);
    this.setData({ attrKey: e.detail.value, });
  },

  // 属性值
  toAttrVal: function (e) {
    console.log(e);
    this.setData({ attrVal: e.detail.value, });
  },

  // 修改数据
  toAttrSave: function (e) {
    var _this = this;
    var formData = {
      tmp_id: this.data.id,
      attr_id: this.data.attrId,
      attr_key: this.data.attrKey,
      attr_val: this.data.attrVal,
    };
    console.log(formData); //return
    wx.request({
      method: 'POST',
      url: app.globalData.xcxApi + 'yw-user/tmp/attr/edit',
      data: formData,
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res); //return
        if (res.code < 0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        _this.getOne(_this.data.code);
        _this.setData({ pop_attr: 0, });
      },
      fail: function () {
        console.log('error');
      }
    });
  },

  // 删除
  toAttrDel: function (e) {
    console.log(e); //return
    var _this = this;
    var formData = {
      attr_id: e.currentTarget.dataset.id,
    };
    wx.request({
      method: 'POST',
      url: app.globalData.xcxApi + 'yw-user/tmp/attr/del',
      data: formData,
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res); //return
        if (res.code < 0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        _this.getOne(_this.data.code);
      },
      fail: function () {
        console.log('error');
      }
    });
  },

  // // 新增
  // toAdd: function (e) {
  //   console.log(e);
  //   var _this = this;
  //   var formData = {
  //   };
  //   wx.request({
  //     method: 'POST',
  //     url: app.globalData.xcxApi + 'yw-user/tmp/attr/add',
  //     data: { cate_id: id, id: this.data.id, },
  //     timeout: 10000, //接口超时10S
  //     success: function (res) {
  //       res = res.data;
  //       console.log(res); //return
  //       if (res.code < 0) {
  //         wx.showToast({
  //           title: res.msg, icon: 'none'
  //         }); return
  //       }
  //       _this.setData({
  //       });
  //     },
  //     fail: function () {
  //       console.log('error');
  //     }
  //   });
  // },
})